package com.joinval.modules.sys.controller;

import com.joinval.common.annotation.SysLog;
import com.joinval.common.enums.ResultCodeEnum;
import com.joinval.common.exception.JoinvalException;
import com.joinval.common.utils.*;
import com.joinval.modules.sys.entity.SysMenuEntity;
import com.joinval.modules.sys.entity.SysUserEntity;
import com.joinval.modules.sys.enums.AuditStatusEnums;
import com.joinval.modules.sys.enums.UserStatusEnums;
import com.joinval.modules.sys.enums.UserTypeEnums;
import com.joinval.modules.sys.form.PasswordForm;
import com.joinval.modules.sys.form.PasswordFormAdmin;
import com.joinval.modules.sys.form.SysUserInfoForm;
import com.joinval.modules.sys.service.SysMenuService;
import com.joinval.modules.sys.service.SysUserService;
import com.joinval.modules.sys.vo.MenuTree;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 系统用户
 *
 * @author Mark sunlightcs@gmail.com
 */
@Api(description = "系统用户控制器")
@RestController
@RequestMapping("/sys/user")
public class SysUserController extends AbstractController {
    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SysMenuService sysMenuService;


    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo", value = "分页启始值", required = false, paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", required = false, paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "sortField", value = "排序字段", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "sortOrder", value = "排序规则 asc:正排 desc:倒排", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "userType", value = "(0:后台用户,1:志愿者2:点单机构3:,微信普通用户)", required = true, paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "auditStatus", value = "(0:审核失败,1:审核成功,2:待审核)", required = false, paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "volCode", value = "志愿者编号", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "orgCode", value = "机构编号", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "orgName", value = "机构名称", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "realName", value = "姓名", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "mobile", value = "手机号", required = false, paramType = "query", dataType = "String")
    })
    @ApiOperation(value = "用户下拉菜单")
    @GetMapping("/menuList")
    public Result menuList(@RequestParam(value = "pageNo", required = false, defaultValue = "0") Integer pageNo,
                           @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize,
                           @RequestParam(value = "sortField", required = false, defaultValue = "createTime") String sortField,
                           @RequestParam(value = "sortOrder", required = false, defaultValue = "desc") String sortOrder,
                           @RequestParam(value = "userType", required = true) String userType,
                           @RequestParam(value = "auditStatus", required = false) Integer auditStatus,
                           @RequestParam(value = "volCode", required = false) String volCode,
                           @RequestParam(value = "orgCode", required = false) String orgCode,
                           @RequestParam(value = "orgName", required = false) String orgName,
                           @RequestParam(value = "realName", required = false) String realName,
                           @RequestParam(value = "mobile", required = false) String mobile
    ) {
        PageUtils pageUtils = sysUserService.menuList(PageUtils.parsePageInfo(pageNo, pageSize, sortField, sortOrder), UserTypeEnums.getEnum(userType), AuditStatusEnums.getEnum(auditStatus), orgCode ,volCode, orgName, realName, mobile);
        return Result.ok().data(pageUtils);
    }


    @ApiOperation(value = "二维码", notes = "动态二维码")
    @PostMapping("/dynamicQRCode/{userId}")
    public void dynamicQRCode(@PathVariable Long userId, HttpServletResponse response) {
        sysUserService.dynamicQRCode(userId,response);
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id主键", required = true, paramType = "query", dataType = "Long")
    })
    @ApiOperation(value = "用户中心", notes = "用户中心个人信息")
    @GetMapping("/volUserCenter")
    public Result volUserCenter(@RequestParam(value = "userId", required = true) Long userId,
                                @RequestParam(value = "teamIds", required = false) Set<Long> teamIds,
                                @RequestParam(value = "leaderStatus", required = false) Integer leaderStatus) {
        return sysUserService.volUserCenter(userId, teamIds, leaderStatus);
    }


    @ApiImplicitParam(name = "userId", value = "志愿者主键", required = true, paramType = "query", dataType = "Long")
    @ApiOperation(value = "详情")
    @GetMapping("/volUserInfo/{userId}")
    public Result volUserInfo(@PathVariable Long userId) {
        Map<String, Object> volUserInfo = sysUserService.volUserInfo(userId);
        return Result.ok().data(volUserInfo);
    }

    @ApiImplicitParam(name = "userId", value = "点单机构主键", required = true, paramType = "query", dataType = "Long")
    @ApiOperation(value = "详情")
    @GetMapping("/volOrgInfo/{userId}")
    public Result volOrgInfo(@PathVariable Long userId) {
        Map<String, Object> volOrgInfo = sysUserService.volOrgInfo(userId);
        return Result.ok().data(volOrgInfo);
    }


    @ApiOperation(value = "机构列表")
    @GetMapping("/orgList")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo", value = "分页启始值", required = false, paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", required = false, paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "sortField", value = "排序字段", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "sortOrder", value = "排序规则 asc:正排 desc:倒排", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "auditStatus", value = "审核状态(0:未通过,1:已通过)", required = false, paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "orgName", value = "机构名称", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "orgLocation", value = "机构地址", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "orgType", value = "机构类型", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "realName", value = "姓名", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "mobile", value = "手机号", required = false, paramType = "query", dataType = "String")

    })
    public Result orgList(@RequestParam(value = "pageNo", required = false, defaultValue = "0") Integer pageNo,
                          @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize,
                          @RequestParam(value = "sortField", required = false, defaultValue = "createTime") String sortField,
                          @RequestParam(value = "sortOrder", required = false, defaultValue = "desc") String sortOrder,
                          @RequestParam(value = "auditStatus", required = false) Integer auditStatus,
                          @RequestParam(value = "orgName", required = false) String orgName,
                          @RequestParam(value = "orgLocation", required = false) String orgLocation,
                          @RequestParam(value = "orgType", required = false) String orgType,
                          @RequestParam(value = "realName", required = false) String realName,
                          @RequestParam(value = "mobile", required = false) String mobile

    ) {
        PageUtils page = sysUserService.orgList(PageUtils.parsePageInfo(pageNo, pageSize, sortField, sortOrder), AuditStatusEnums.getEnum(auditStatus),orgName, orgLocation, orgType, realName, mobile);
        return Result.ok().data(page);
    }

    @ApiOperation(value = "列表")
    @GetMapping("/volList")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo", value = "分页启始值", required = false, paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", required = false, paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "sortField", value = "排序字段", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "sortOrder", value = "排序规则 asc:正排 desc:倒排", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "auditStatus", value = "审核状态(0:未通过,1:已通过)", required = false, paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "minAge", value = "最小年龄", required = false, paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "maxAge", value = "最大年龄", required = false, paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "minServiceHour", value = "最短服务时长", required = false, paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "maxServiceHour", value = "最长服务时长", required = false, paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "volCode", value = "志愿者编号", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "realName", value = "姓名", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "mobile", value = "手机号", required = false, paramType = "query", dataType = "String")
    })
    public Result volList(@RequestParam(value = "pageNo", required = false, defaultValue = "0") Integer pageNo,
                          @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize,
                          @RequestParam(value = "sortField", required = false, defaultValue = "createTime") String sortField,
                          @RequestParam(value = "sortOrder", required = false, defaultValue = "desc") String sortOrder,
                          @RequestParam(value = "auditStatus", required = false) Integer auditStatus,
                          @RequestParam(value = "minAge", required = false) Integer minAge,
                          @RequestParam(value = "maxAge", required = false) Integer maxAge,
                          @RequestParam(value = "minServiceHour", required = false) Integer minServiceHour,
                          @RequestParam(value = "maxServiceHour", required = false) Integer maxServiceHour,
                          @RequestParam(value = "volCode", required = false) String volCode,
                          @RequestParam(value = "realName", required = false) String realName,
                          @RequestParam(value = "mobile", required = false) String mobile

    ) {
        PageUtils page = sysUserService.volList(PageUtils.parsePageInfo(pageNo, pageSize, sortField, sortOrder), AuditStatusEnums.getEnum(auditStatus), minAge, maxAge, minServiceHour, maxServiceHour, volCode, realName, mobile);
        return Result.ok().data(page);
    }

    @ApiOperation(value = "登录用户个人信息",notes =
            "cityId:所属市, countyId:所属区县id, townId:所属街镇id逗号分割, townIdList:所属街镇id数组")
    @GetMapping("/loginUserInfo")
    public Result loginUserInfo() {
        Long userId = ShiroUtils.getUserId();
        Map<String, Object> userInfo = sysUserService.loginUserInfo(userId);
        List<SysMenuEntity> sysMenuEntities = sysMenuService.myMenu(userId);
        List<MenuTree> menuTrees = MenuTree.buildTree(sysMenuEntities);
        List<String> perms = sysUserService.myPerms(userId);
        userInfo.put("menuTrees", menuTrees);
        userInfo.put("perms", perms);
        //角色
        userInfo.put("roles", sysUserService.getUserRolesByUserId(userId));

        // 街道信息
        String townIdRes = userInfo.get("townId")==null? null : userInfo.get("townId").toString();
        userInfo.put("townIdList", townIdRes==null ? null : StringUtils.split(townIdRes, ","));

        return Result.ok().data(userInfo);
    }

    @ApiOperation(value = "后台用户列表")
    @GetMapping("/list")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo", value = "分页启始值", required = false, paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", required = false, paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "sortField", value = "排序字段", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "sortOrder", value = "排序规则 asc:正排 desc:倒排", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "status", value = "用户状态(0:禁用,1:正常)", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "gender", value = "性别(0-男, 1-女)", required = false, paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "idCard", value = "身份证", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "username", value = "账户", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "realName", value = "真实姓名", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "mobile", value = "手机号", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "countyId", value = "区域id", required = false, paramType = "query", dataType = "Long"),
            @ApiImplicitParam(name = "townId", value = "街镇id", required = false, paramType = "query", dataType = "Long"),
            @ApiImplicitParam(name = "roleId", value = "角色ID", required = false, paramType = "query", dataType = "Long")
    })
    public Result list(@RequestParam(value = "pageNo", required = false, defaultValue = "0") Integer pageNo,
                       @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize,
                       @RequestParam(value = "sortField", required = false, defaultValue = "createTime") String sortField,
                       @RequestParam(value = "sortOrder", required = false, defaultValue = "desc") String sortOrder,
                       @RequestParam(value = "status", required = false) Integer status,
                       @RequestParam(value = "gender", required = false) Integer gender,
                       @RequestParam(value = "idCard", required = false) String idCard,
                       @RequestParam(value = "username", required = false) String username,
                       @RequestParam(value = "realName", required = false) String realName,
                       @RequestParam(value = "mobile", required = false) String mobile,
                       @RequestParam(value = "countyId", required = false) Long countyId,
                       @RequestParam(value = "townId", required = false) Long townId,
                       @RequestParam(value = "roleId", required = false) Long roleId

    ) {
        PageUtils page = sysUserService.list(PageUtils.parsePageInfo(pageNo, pageSize, sortField, sortOrder),
                UserStatusEnums.getEnum(status), gender, idCard, username, realName, mobile,countyId,townId,roleId);
        return Result.ok().data(page);
    }

    @ApiOperation(value = "根据角色编码查询用户")
    @GetMapping("/getUserListPageByRoleCode")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo", value = "分页启始值", required = false, paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", required = false, paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "sortField", value = "排序字段", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "sortOrder", value = "排序规则 asc:正排 desc:倒排", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "status", value = "用户状态(0:禁用,1:正常)", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "gender", value = "性别(0-男, 1-女)", required = false, paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "idCard", value = "身份证", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "username", value = "账户", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "realName", value = "真实姓名", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "mobile", value = "手机号", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "roleCode", value = "角色编号", required = false, paramType = "query", dataType = "String")
            ,@ApiImplicitParam(name = "countyId", value = "区id", required = false, paramType = "query", dataType = "Long")
            ,@ApiImplicitParam(name = "townId", value = "街id", required = false, paramType = "query", dataType = "Long")
    })
    public Result getUserListPageByRoleCode(@RequestParam(value = "pageNo", required = false, defaultValue = "0") Integer pageNo,
                       @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize,
                       @RequestParam(value = "sortField", required = false, defaultValue = "createTime") String sortField,
                       @RequestParam(value = "sortOrder", required = false, defaultValue = "desc") String sortOrder,
                       @RequestParam(value = "status", required = false) Integer status,
                       @RequestParam(value = "gender", required = false) Integer gender,
                       @RequestParam(value = "idCard", required = false) String idCard,
                       @RequestParam(value = "username", required = false) String username,
                       @RequestParam(value = "realName", required = false) String realName,
                       @RequestParam(value = "mobile", required = false) String mobile,
                       @RequestParam(value = "roleCode", required = false) String roleCode
            ,@RequestParam(value = "countyId",required = false) Long countyId
            ,@RequestParam(value = "townId",required = false) Long townId

    ) {
        PageUtils page = sysUserService.getUserListPageByRoleCode(PageUtils.parsePageInfo(pageNo, pageSize, sortField, sortOrder)
                , UserStatusEnums.getEnum(status), gender, idCard, username, realName, mobile
                , roleCode
                , countyId
                , townId
                ,null);
        return Result.ok().data(page);
    }

    @ApiOperation(value = "获取检查员列表-根据当前登录用户配置的行政范围")
    @GetMapping("/getInspectorByNowUser")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo", value = "分页启始值", required = false, paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", required = false, paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "sortField", value = "排序字段", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "sortOrder", value = "排序规则 asc:正排 desc:倒排", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "status", value = "用户状态(0:禁用,1:正常)", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "gender", value = "性别(0-男, 1-女)", required = false, paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "idCard", value = "身份证", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "username", value = "账户", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "realName", value = "真实姓名", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "mobile", value = "手机号", required = false, paramType = "query", dataType = "String")
    })
    public Result getInspectorByNowUser(@RequestParam(value = "pageNo", required = false, defaultValue = "0") Integer pageNo,
                       @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize,
                       @RequestParam(value = "sortField", required = false, defaultValue = "createTime") String sortField,
                       @RequestParam(value = "sortOrder", required = false, defaultValue = "desc") String sortOrder,
                       @RequestParam(value = "status", required = false) Integer status,
                       @RequestParam(value = "gender", required = false) Integer gender,
                       @RequestParam(value = "idCard", required = false) String idCard,
                       @RequestParam(value = "username", required = false) String username,
                       @RequestParam(value = "realName", required = false) String realName,
                       @RequestParam(value = "mobile", required = false) String mobile

    ) {
        Long countyId = null;
        List<Long> townIdList = new ArrayList<>();

        SysUserEntity user = ShiroUtils.getUserEntity();

//        List<String> roleCodes = sysUserService.getUserRoleCodesByUserId(ShiroUtils.getUserId());
//        if (roleCodes.contains(RoleCode.CITY_COMMERCE.getCode())
//                || roleCodes.contains(RoleCode.ENTERPRISE.getCode())
//                || roleCodes.contains(RoleCode.PLATFORM_OPERATION.getCode())
//                || roleCodes.contains(RoleCode.ADMIN.getCode())){
//
//        }else if (StringUtils.isNotEmpty(nowUserCountyId) || StringUtils.isNotEmpty(nowUserTownIds)){
//            if (StringUtils.isNotEmpty(nowUserCountyId)  ) {
//                countyId = Long.valueOf(nowUserCountyId);
//            }
//            if (StringUtils.isNotEmpty(nowUserTownIds)  ) {
//                String[] arr = org.apache.commons.lang3.StringUtils.split(nowUserTownIds, ",");
//                List<Long> list = Arrays.asList(arr).stream().map(e-> Long.valueOf(e)).collect(Collectors.toList());
//                townIdList.addAll(list);
//            }
//
//        }else {
//            throw  new JoinvalException("无权限");
//        }

        PageUtils page = sysUserService.getUserListPageByRoleCode(PageUtils.parsePageInfo(pageNo, pageSize, sortField, sortOrder)
                , UserStatusEnums.getEnum(status), gender, idCard, username, realName, mobile
                , null
                , countyId, null, townIdList);
        return Result.ok().data(page);
    }

    @SysLog("新建用户")
    @ApiOperation(value = "新建用户")
    @PostMapping("/add")
    public Result add(@RequestBody SysUserEntity user) {
        sysUserService.saveUser(user);
        return Result.ok();
    }

    @SysLog("后台管理更新用户信息")
    @ApiOperation(value = "后台管理更新用户信息")
    @PostMapping("/update")
    public Result update(@RequestBody SysUserEntity user) {
        sysUserService.update(user);
        return Result.ok();
    }

    @SysLog("用户自己更新信息")
    @ApiOperation(value = "用户自己更新信息-new")
    @PostMapping("/updateOwnInfo")
    public Result updateOwnInfo(@RequestBody SysUserEntity user) {
        sysUserService.updateOwnInfo(user);
        return Result.ok();
    }

    @SysLog("修改密码")
    @ApiOperation(value = "自行修改密码")
    @PostMapping("/setPwd")
    public Result setPwd(@RequestBody @Valid PasswordForm form) {
        if(!RegularUtils.isPasswordLength(form.getNewPassword())) {
            return Result.error(ResultCodeEnum.PWD_LENGTH_ERROR);
        }
        //sha256加密
        String password = new Sha256Hash(form.getPassword(), getUser().getSalt()).toHex();
        //sha256加密
        String newPassword = new Sha256Hash(form.getNewPassword(), getUser().getSalt()).toHex();
        //更新密码
        boolean flag = sysUserService.updatePassword(getUserId(), password, newPassword);
        if (!flag) {
            return Result.error("原密码不正确");
        }
        return Result.ok();
    }

    @SysLog("管理员修改密码")
    @ApiOperation(value = "管理员修改密码")
    @PostMapping("/setPwdByAdmin")
    public Result setPwdByAdmin(@RequestBody PasswordFormAdmin form) {
        SysUserEntity user = sysUserService.getById(form.getUserId());
        if(!RegularUtils.isPasswordLength(form.getNewPassword())) {
            return Result.error(ResultCodeEnum.PWD_LENGTH_ERROR);
        }
        String passwordNew = new Sha256Hash(form.getNewPassword(), user.getSalt()).toHex();
        user.setPassword(passwordNew);
        sysUserService.updateById(user);
        return Result.ok();
    }

    @SysLog("删除用户")
    @ApiOperation(value = "删除用户")
    @PostMapping("/delete")
    public Result delete(@RequestBody Long[] userIds) {
        if (ArrayUtils.contains(userIds, 1L)) {
            return Result.error("系统管理员不能删除");
        }
        if (ArrayUtils.contains(userIds, getUserId())) {
            return Result.error("当前用户不能删除");
        }
        sysUserService.deleteBatch(userIds);
        return Result.ok();
    }

    @SysLog("启用，禁用账户")
    @ApiOperation(value = "启用，禁用账户")
    @PostMapping("/setUserStatus/{userId}")
    public Result enable(@PathVariable Long userId) {
        if (userId == Constant.SUPER_ADMIN) {
            return Result.error("系统管理员不能禁用");
        }
        SysUserEntity user = sysUserService.getById(userId);
        user.setStatus(UserStatusEnums.DISABLE.value().equals(user.getStatus()) ? UserStatusEnums.ENABLE.value() : UserStatusEnums.DISABLE.value());
        sysUserService.updateById(user);
        return Result.ok();
    }


    @ApiOperation(value = "用户详情-new")
    @GetMapping("/bgUserInfo/{userId}")
    public Result bgUserInfo(@PathVariable Long userId) {
        Map<String, Object> userInfo = sysUserService.bgUserInfo(userId);
        return Result.ok().data(userInfo);
    }

    @ApiOperation(value = "点单机构修改")
    @PostMapping(value = "/orgUpdate")
    public Result orgUpdate(@RequestBody SysUserInfoForm sysUserInfoForm) {
        sysUserService.orgUpdate(sysUserInfoForm);
        return Result.ok();
    }

    @ApiOperation(value = "上传头像")
    @PostMapping(value = "/logo")
    public Result logo(@RequestBody Map<String,Object> params) {
        Long userId = (Long)params.get("userId");
        String logo = (String)params.get("logo");
        sysUserService.logo(userId,logo);
        return Result.ok();
    }

}
